package model

import (
	"common/global"
	"time"
)

type OrderSeckill struct {
	Id            int64     `gorm:"column:id;type:bigint;comment:主键ID;primaryKey;not null;" json:"id"`                                // 主键ID
	OrderId       int64     `gorm:"column:order_id;type:bigint;comment:关联主订单ID;not null;" json:"order_id"`                            // 关联主订单ID
	ActivityId    int64     `gorm:"column:activity_id;type:mediumint;comment:秒杀活动ID;not null;" json:"activity_id"`                    // 秒杀活动ID
	SeckillPrice  float32   `gorm:"column:seckill_price;type:decimal(8, 2);comment:秒杀价格;not null;" json:"seckill_price"`              // 秒杀价格
	OriginalPrice float32   `gorm:"column:original_price;type:decimal(8, 2);comment:原价;not null;" json:"original_price"`              // 原价
	Status        int64     `gorm:"column:status;type:tinyint;comment:秒杀订单状态： 1已支付 2未支付 3待支付 4已取消;not null;default:0;" json:"status"` // 秒杀订单状态: 1已支付 2未支付 3待支付 4已取消
	CreatedAt     time.Time `gorm:"column:created_at;type:datetime;comment:创建时间;not null;" json:"created_at"`                         // 创建时间
	UpdatedAt     time.Time `gorm:"column:updated_at;type:datetime;comment:更新时间;not null;" json:"updated_at"`                         // 更新时间
}

func (OrderSeckill) TableName() string {
	return "order_seckill"
}

// GetSeckillOrderByOrderId 根据主订单ID查询秒杀订单
func GetSeckillOrderByOrderId(orderId int64) (result *OrderSeckill, err error) {
	err = global.Db.Where("order_id = ?", orderId).Limit(1).Find(&result).Error
	if err != nil {
		return nil, err
	}
	return result, nil
}

func SeckillOrderCreate(order *OrderSeckill) error {
	err := global.Db.Create(&order).Error
	if err != nil {
		return err
	}
	return nil
}

func SeckillOrderDelete(master *OrderMaster) {
	global.Db.Delete(&OrderSeckill{}, "order_id = ?", master.Id)
}
